package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.FdProduct;
import com.ruoyi.system.service.IFdProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 产品信息Controller
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@RestController
@RequestMapping("/system/product")
public class FdProductController extends BaseController
{
    @Autowired
    private IFdProductService fdProductService;

    /**
     * 查询产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(FdProduct fdProduct)
    {
        startPage();
        List<FdProduct> list = fdProductService.selectFdProductList(fdProduct);
        return getDataTable(list);
    }

    /**
     * 导出产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:export')")
    @Log(title = "产品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FdProduct fdProduct)
    {
        List<FdProduct> list = fdProductService.selectFdProductList(fdProduct);
        ExcelUtil<FdProduct> util = new ExcelUtil<FdProduct>(FdProduct.class);
        util.exportExcel(response, list, "产品信息数据");
    }


    /**
     * 下载产品信息导入模板
     */
    @PreAuthorize("@ss.hasPermi('system:product:importTemplate')")
    @Log(title = "产品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        // 创建 ExcelUtil 实例，使用钻石操作符简化泛型声明
        ExcelUtil<FdProduct> util = new ExcelUtil<>(FdProduct.class);
        // 准备空数据列表，用于生成模板
        List<FdProduct> list = new ArrayList<>();
        // 调用 exportExcel 方法导出模板文件
        util.exportExcel(response, list, "产品导入模板", "产品导入模板");
    }

    /**
     * 导入产品信息
     */
    @PreAuthorize("@ss.hasPermi('system:product:importForm')")
    @Log(title = "产品信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) throws Exception {
        ExcelUtil<FdProduct> util = new ExcelUtil<FdProduct>(FdProduct.class);
        List<FdProduct> productList = util.importExcel(file.getInputStream(),1);
        String message = fdProductService.importFdProduct(productList);
        return AjaxResult.success(message);
    }

    /**
     * 获取产品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:product:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(fdProductService.selectFdProductByProductId(productId));
    }

    /**
     * 新增产品信息
     */
    @PreAuthorize("@ss.hasPermi('system:product:add')")
    @Log(title = "产品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FdProduct fdProduct)
    {
        return toAjax(fdProductService.insertFdProduct(fdProduct));
    }

    /**
     * 修改产品信息
     */
    @PreAuthorize("@ss.hasPermi('system:product:edit')")
    @Log(title = "产品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FdProduct fdProduct)
    {
        return toAjax(fdProductService.updateFdProduct(fdProduct));
    }

    /**
     * 删除产品信息
     */
    @PreAuthorize("@ss.hasPermi('system:product:remove')")
    @Log(title = "产品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(fdProductService.deleteFdProductByProductIds(productIds));
    }
}
